using System;
using System.Collections;
using System.IO;
using fit;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestUtils
{
    public abstract class UnitFitRunner
    {
        protected Hashtable dictionary;

        public UnitFitRunner()
        {
            dictionary = CreateDictionary();
            ContextManager.TestContext = CreateContext();
        }

        private int RunTheTest(string inFile, string outFile, Hashtable fixtureDictionary)
        {
            if (File.Exists(outFile))
            {
                File.SetAttributes(outFile, FileAttributes.Normal);
            }
            FileRunner runner = new FileRunner();
            runner.Dictionary = fixtureDictionary;
            string directory = Environment.CurrentDirectory;
            runner.args(new string[] {inFile, outFile, directory});
            runner.process();
            runner.output.Close();
            File.SetAttributes(outFile, FileAttributes.ReadOnly);
            return runner.fixture.counts.exceptions > 0 ? runner.fixture.counts.exceptions : runner.fixture.counts.wrong;
        }

        protected virtual Context CreateContext()
        {
            return new Context();
        }

        [TestMethod]
        public void TestRun()
        {
            Assert.AreEqual(0, RunTheTest(FullInputName, FullOutputName, dictionary));
        }

        protected virtual Hashtable CreateDictionary()
        {
            return new Hashtable();
        }

        private string FullOutputName
        {
            get { return OutFolder + (ResultPrefix == String.Empty ? String.Empty : ResultPrefix + '_') + TestName; }
        }

        private string FullInputName
        {
            get { return InFolder + TestName; }
        }

        protected virtual string ResultPrefix
        {
            get { return string.Empty; }
        }

        protected abstract string TestName { get; }
        protected abstract string InFolder { get; }
        protected abstract string OutFolder { get; }
    }
}